import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home,
        redirect: 'index',
        children: [
            {
                path: '/WarehousingOPT',
                name: 'WarehousingOPT',
                component: () => import('../components/WarehousingOPT.vue')
            },
            {
                path: '/PickingOPT_Emergent',
                name: 'PickingOPT_Emergent',
                component: () => import('../components/PickingOPT_Emergent.vue')
            },
            {
                path: '/InventoryOPT',
                name: 'InventoryOPT',
                component: () => import('../components/InventoryOPT.vue')
            },
            {
                path: '/ReplenishmentOPT',
                name: 'ReplenishmentOPT',
                component: () => import('../components/ReplenishmentOPT.vue')
            },
            {
                path: '/PickingOPT',
                name: 'PickingOPT',
                component: () => import('../components/PickingOPT.vue')
            },
            {
                path: '/RetrieveBox',
                name: 'RetrieveBox',
                component: () => import('../components/RetrieveBox.vue')
            },
            {
                path: '/WarehousingOPT_WC',
                name: 'WarehousingOPT_WC',
                component: () => import('../components/WarehousingOPT_WC.vue')
            },
            {
                path: '/LinePatrolPickingOPT',
                name: 'LinePatrolPickingOPT',
                component: () => import('../components/LinePatrolPickingOPT.vue')
            },
            {
                path: '/TallyOpt',
                name: 'TallyOpt',
                component: () => import('../components/TallyOpt.vue')
            }
        ]
    },
    {
        path: '/index',
        name: 'index',
        component: () => import('../views/Index.vue')
    },
    {
        path: '/Login',
        name: 'Login',
        component: () => import('../views/Login.vue')
    }
]

const router = new VueRouter({
    routes
})  

// 配置路由守卫
router.beforeEach((to, from, next) => {
    // 如果访问登陆页，则放行
    if (to.path === '/Login') {
        return next()
    }   
    // 如果用户未登陆，则跳转到/login
    const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
    if (!userInfo) {
        return next('/Login')
    }
    // 如果用户已登陆，则放行
    next()
})

export default router
